require(['jquery',"bootstrap","bbcare",'scprit/patientsQuestionnaire/vm/common.js',
    "scprit/patientsQuestionnaire/vm/womenHealthQuestionnaire.ui.js",
    "scprit/patientsQuestionnaire/vm/womenHealthQuestionnaire.servers.js",
    "scprit/patientsQuestionnaire/vm/womenHealthQuestionnaire.event.js"
    ],function($,bootstrap,$bb,$common,$ui,$servers,$event){
    var getInfo = JSON.parse(localStorage.getItem("patientInfo"));
    //localStorage.removeItem("patientInfo");
    /*
    * 获取问卷：
    * */
    var inParams = {"userId":getInfo.userId,"queueId":getInfo.queueId,"evaType":getInfo.evaType,"deptId":getInfo.deptId};//getInfo.evaType
    var params= $.extend(inParams,{});
    var getQuestionCallback = function(data,params){
        if(data.success){
            if(params.evaType =="LES"){
                $ui.instructionInit({"id":'#box',"data":data.data});
                var lesData = data.data["生活事件量表"];
                var scoreObj = {"mark":{},"ques":{},'manyTimes':{}};
                //题目格式处理：
                var totalQuestions = getTotalQuestions_les(lesData);
                var questions = getQuestions_les(totalQuestions);
                //点击开始答题：
                $("#toStart").click(function(){
                    $ui.layoutBoxInit({"id":'#box'});
                    $ui.layoutInit({"id":"#wrap"});
                    $ui.projectScheduleInit({"id":"#wrap_right","data":lesData});
                    $ui.btnGroupLesInit({"id":"#ques-click-group-les","data":lesData,"lens":lesData[0].lens});
                    $ui.questionTypeInit({"id":"#questionnaire","data":questions["1.1"]});//默认展示第一题：
                });
                var quesLens = totalQuestions.length;
                $event.init(questions,scoreObj,quesLens,params);

            }else if(params.evaType =="OSI"){

                var scoreObj = {"ques":{},"mark":{}};
                var questions = data.data.quetions;
                $ui.layoutBoxInit({"id":'#box'});
                $ui.quesTabInit({"id":"#ques-tabs","type":params.evaType});//顶部题目类型切换
                $ui.layoutInit({"id":"#wrap"});
                var totalQuestions = getTotalQuestions_osi(questions,params.evaType);
                var questions2 = getQuestions_osi(totalQuestions);
                $ui.projectScheduleInit({"id":"#wrap_right","data":questions});
                //渲染下方按钮组：
                $ui.btnGroupOsiInit({"id":"#ques-click-group-osi","data":totalQuestions,"lens":totalQuestions[0].length,"type":params.evaType});
                $event.btnGroupCss(1,totalQuestions.length);
                 //默认展示第一题：
                $ui.questionTypeInit2({"id":"#questionnaire","data":questions2[1]});
                $event.init2(questions2,scoreObj,params,totalQuestions.length);
                $("#ques-click-group-osi").find('button[type][item="'+questions2[1].listNo+'"]').addClass("btn-checked");
            
            }else if(params.evaType =="GLE"){//妇女健康调查问卷：
                var total = [];
                var arrItem = ["一般人口学信息","月经婚育史","既往避孕措施情况","近1年内是否出现以下症状","妇科及既往疾病史","家族史","是否诊断为卵巢早衰或卵巢功能减退"];
                for(var i = 0;i < arrItem.length ; i++){
                    total.push({"title":arrItem[i],"questions":data.data[arrItem[i]],"group":arrItem[i]});
                }
                getTotalQuestions_osiList(total);
                var scoreObj = {"ques":{},"mark":{}};
                $ui.layoutBoxInit({"id":'#box'});
                $ui.quesTabInit({"id":"#ques-tabs","type":params.evaType});//顶部题目类型切换
                $ui.layoutInit({"id":"#wrap"});
                var totalQuestions = getTotalQuestions_osi(total,params.evaType);
                var questions3 = getQuestions_osi(totalQuestions);
                
                $ui.projectScheduleInit({"id":"#wrap_right","data":total});
                $ui.btnGroupOsiInit({"id":"#ques-click-group-osi","data":totalQuestions,"lens":totalQuestions[0].length,"type":params.evaType});
                $event.btnGroupCss(1,totalQuestions.length);
                //默认展示第一题：
                $ui.questionTypeInit2({"id":"#questionnaire","data":questions3[1]});
                $event.init2(questions3,scoreObj,params,totalQuestions.length);
                $("#ques-click-group-osi").find('button[type][item="'+questions3[1].listNo+'"]').addClass("btn-checked");
            }
            /*
            *   date类型的输入控制在min 和 max之间
             */
            $("#box").on("input propertychange","input[type='date']",function(){
                var min = $(this).attr("min");
                var max = $(this).attr("max");
                var value = $(this).val();
                if(max && value){
                    var maxDiff = $common.dateDiff2(value,  max);
                    if(maxDiff*1 > 0){
                        $common.toastShow("请选择"+ max +"之前的日期",1500);
                        $(this).val('');
                        return;
                    }
                }
                if(min && value){
                    var minDiff = $common.dateDiff2(value,  min);
                    if(minDiff*1 < 0){
                        $common.toastShow("请选择"+ min +"之后的日期",1500);
                        $(this).val('');
                        return;
                    }
                }
            });
        }else{
            $("#box").html("请求失败……");
        }
    };
    $servers.getQuestion(inParams,getQuestionCallback,params);
    /*
    * les问卷处理：
     */
    var getTotalQuestions_les = function(lesData){
        var totalQuestions = [];
        for(var i = 0;i< lesData.length ; i++){//大层级：
            for(var j = 0;j< lesData[i].questions.length;j++){//每一个大层级的题目：
                lesData[i].questions[j].itemTitle = lesData[i].title;
                //获取每个小问卷的题目；
                var ques = lesData[i].questions[j].questions;
                for(var k = 0;k<ques.length;k++){  //每一个题目的小选项：
                    ques[k].fuId = lesData[i].questions[j].id;
                    ques[k].itemTitle = lesData[i].title;
                    ques[k].title = lesData[i].questions[j].title;
                    ques[k].lens = lesData[i].questions.length;
                    ques[k].indexNo = j+1;
                }
                //lesData[i].questions[j].lens = lesData[i].questions.length;
                totalQuestions.push(lesData[i].questions[j]);
            }
            lesData[i].lens = lesData[i].questions.length; 
        }
        return totalQuestions;
    }
    var getQuestions_les = function(totalQuestions){
        var questions = {};
        for(var i  = 0;i<totalQuestions.length;i++ ){
            for(var k = 0;k<totalQuestions[i].questions.length;k++){
                var item = totalQuestions[i].questions[k].fuId + "."+ totalQuestions[i].questions[k].id;
                totalQuestions[i].questions[k].maxTime = $common.getNowFormatDate();
                questions[item] = totalQuestions[i].questions[k];
            }
        }
        return questions;
    }
    /*
    * osi问卷
     */
    var listDeal = function(list,ques){
        var ss= [];
        for(var m = 0;m<list.length;m++) {
            if(list[m].list) {
                var listS = list[m].list;
                for(var i = 0;i < listS.length;i++){
                    if(listS[i].list){
                        for(var j = 0;j < listS[i].list.length;j++){
                            listS[i].list[j].css ="none";
                            ss.push(listS[i].list[j]);
                        }
                    }else{
                        ss.push(listS[i]);
                    }
                }
            }else{
                ss.push(list[m]);
            }
        }
        ques.list = ss;
    };
    var getTotalQuestions_osiList = function(questions){
        for(var i = 0; i< questions.length;i++){
            for(var k = 0;k<questions[i].questions.length;k++){
                if(questions[i].questions[k].list){
                    listDeal(questions[i].questions[k].list, questions[i].questions[k]);
                }
            }
        }
    };
    var topicType = function(des1,list){
        var des3 = [];
        for(var j = 0;j<des1.length;j++){
            var text2;
            if(des1[j].indexOf("text") == 0){
                text2 = "text";
                if(des1[j].indexOf("-") >0){
                    var leiXing2 = des1[j].substring(des1[j].indexOf("-")+1);
                    if(leiXing2 == "boxes"){
                        list.type = "blankList";//需要填入多条；
                    }else if(leiXing2 == "num"){
                        text2 = "number";
                    }else if(leiXing2 == "time"){
                        text2 = "time";
                    }else if(leiXing2 == "date"){
                        text2 = "date";
                    }
                }
            }else{
                text2 = des1[j];
            }
            if(text2 == "date"){
                des3.push({"item":text2,"maxDate":$common.getNowFormatDate()});
            }else{
                des3.push({"item":text2});
            }
        }
        return des3;
    }
    var getTotalQuestions_osi = function(questions,type){
        var totalQuestions = [];
        for(var i = 0; i< questions.length;i++){
            var lens = questions[i].questions.length;
            questions[i].lens = lens;
            for(var k = 0;k<questions[i].questions.length;k++){
                questions[i].questions[k].length = lens;
                var bak = [];
                if(questions[i].questions[k].bak){
                    bak.push({"mark":questions[i].questions[k].bak});
                }
                questions[i].questions[k].itemTitle = questions[i].title;//获取每个group的问卷长度：
                if(questions[i].questions[k].list){//联动类题型获取id；
                    var childId = questions[i].questions[k].list[0].id;
                    /*if(childId.indexOf("-") >= 0){
                        questions[i].questions[k].id=childId.substring(0,childId.indexOf("-"));
                    }else{*/
                        questions[i].questions[k].id=childId;
                    /*}*/
                    var list = questions[i].questions[k].list;
                    var link=[],linkArr = [];//关联的
                    for(var m = 0;m<list.length;m++){
                        var linkId = list[m].id;
                        if($.inArray(linkId,linkArr) >= 0){
                            list[m].css = "none";
                        }
                        if(list[m].option){//选择：
                            if(list[m].choice && list[m].choice != "redio"){
                                list[m].type = "checkbox";
                                if(type == "GLE"){
                                    /*if(list[m].id && list[m].id.indexOf(".")>= 0){
                                    }*/
                                    list[m].description = list[m].id +" "+ list[m].description;
                                }
                            }else{
                                list[m].type = "single";
                                if(type == "GLE"){
                                    //题目带输入框：
                                    if(typeof list[m].description == "object"){
                                        list[m].description.unshift(list[m].id);
                                        if(list[m].id == "2.13.5"){
                                            list[m].description.splice(4,2);
                                        }
                                        list[m].description = topicType(list[m].description,list[m]);
                                        list[m].type = "blankSingle";
                                    }else{
                                        list[m].description = list[m].id + " " + list[m].description;
                                        list[m].type = "single";
                                    }
                                }
                            }
                            if(list[m].choose && typeof list[m].choose == 'object'){
                                linkArr = linkArr.concat(list[m].link0);
                                linkArr = linkArr.concat(list[m].link1);
                                linkArr = link.concat(linkArr);
                                list[m].choose = JSON.stringify(list[m].choose);
                                list[m].link0 = JSON.stringify(list[m].link0);
                                list[m].link1 = JSON.stringify(list[m].link1);
                            }
                            if(list[m].link){
                                linkArr = linkArr.concat(list[m].link);
                                list[m].link = JSON.stringify(list[m].link);
                            }
                            list[m].option = optionDeal(list[m].option,type)
                        }else{
                            list[m].type = "blank";//填空：
                            var des1 = list[m].description;
                            if(list[m].id && list[m].id.indexOf(".")>= 0){
                                des1.unshift(list[m].id+" ");
                            }
                            if(typeof des1 == 'string'){
                                des1 = [des1];
                            }
                            if(des1){
                                list[m].description = topicType(des1,list[m]);
                            }
                        }
                        list[m].itemTitle = questions[i].title;
                        if(list[m].id && list[m].id.indexOf(".")>= 0){
                            var splitId = list[m].id.split(".");
                            list[m].id = splitId.join("-");
                        }
                    }
                    questions[i].questions[k].type = "linkage";//联动类型：
                }else{//
                    if(questions[i].questions[k].option){//选择：
                        if(questions[i].questions[k].choice && questions[i].questions[k].choice != "redio"){
                            questions[i].questions[k].type = "checkbox";
                        }else{
                            questions[i].questions[k].type = "single";
                        }
                        if(questions[i].questions[k].id.indexOf(".")>= 0){
                            questions[i].questions[k].description = questions[i].questions[k].id+" "+questions[i].questions[k].description;
                        }
                        questions[i].questions[k].option = optionDeal(questions[i].questions[k].option,type);
                    }else{
                        questions[i].questions[k].type = "blank";//需要填入多条；
                        var des = questions[i].questions[k].description;
                        if(questions[i].questions[k].id && questions[i].questions[k].id.indexOf(".")>= 0){
                            des.unshift(questions[i].questions[k].id + " ");
                        }
                        questions[i].questions[k].description = topicType(des,questions[i].questions[k]);
                    }
                }
                questions[i].questions[k].backId = questions[i].questions[k].id ;
                if(questions[i].bak){
                    bak.push({"mark":questions[i].bak});
                }
                if(bak.length > 0){
                    questions[i].questions[k].bak = bak ;
                }
                if(questions[i].questions[k].id.indexOf(".")>= 0){
                    var splitId = questions[i].questions[k].id.split(".");
                    questions[i].questions[k].id = splitId.join("-");
                }
                //questions[i].questions[k].extra = [{"num":1},{"num":2}];//备注：
                totalQuestions.push(questions[i].questions[k]);
            }

        }
        return totalQuestions;
    }
    var getQuestions_osi = function(totalQuestions){
        var questions = {};
        for(var i = 0; i< totalQuestions.length;i++){
            totalQuestions[i].listNo = i+1;
            questions[i+1] = totalQuestions[i];
        }
        return questions
    };
    var optionDeal = function(options,type){
        var option = [];
        if(type == 'OSI'){
            var optionArr = ["a","b","c","d","e"];
            for(var i = 0;i < optionArr.length ; i++ ){
                if(options[optionArr[i]]){
                    option.push({"item":optionArr[i],"val":options[optionArr[i]]});
                }
            }
        }else{
            for(var item in options){
                option.push({"item":item,"val":options[item]});
            }
        }
        return option;
    }
});